package com.hsy.baoinbao.modules.login;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.application.BIBApplication;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.login.gsondata.LoginResultData;
import com.hsy.baoinbao.modules.main.ui.activity.MainActivity;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hsy on 2016/3/23.
 */
public class LoginActivity extends CommonBaseActivityWithSimpleActionBar implements View.OnClickListener {
    private EditText mUserNameEditText;
    private EditText mUserPassEditText;
    private Button mLoginButton;
    private TextView mForgetTextView;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                LoginResultData.Result result = DPGsonUtil.fromJson(response, LoginResultData.Result.class);
                if(CommonUtils.INVALID_UID.equals(result.getUid())){
                    showToast(R.string.txt_login_failed);
                    return;
                }
                CommonUtils.saveToken(LoginActivity.this, result.getToken());
                startActivity(MainActivity.class);
                CommonUtils.hideProgressDialog();
                finish();

                BIBApplication.getInstance().registerXGPushService();
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setLeftButtonVisiblity(View.GONE);

        mUserNameEditText = (EditText) findViewById(R.id.user_name);
        mUserPassEditText = (EditText) findViewById(R.id.user_pass);
        mLoginButton = (Button) findViewById(R.id.login);
        mForgetTextView = (TextView) findViewById(R.id.forget);

        mLoginButton.setOnClickListener(this);
        mForgetTextView.setOnClickListener(this);

        setTitle(R.string.login);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                if (!check()) {
                    return;
                }
                CommonUtils.showProgressDialog(this);
                Map<String, String> params = new HashMap<>();
                params.put(Consts.USER_NAME, mUserNameEditText.getText().toString().trim());//username
                params.put(Consts.USER_PASSWORD, mUserPassEditText.getText().toString().trim());//password
                HttpUtil.httpPostWithParams(LoginActivity.this, 0, Consts.API_LOGIN, params, callback);
                break;
            case R.id.forget:
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    private boolean check() {
        if (isEmpty(mUserNameEditText.getText().toString().trim())) {
            showToast(R.string.txt_user_name_cannot_be_empty);
            return false;
        }
        if (isEmpty(mUserPassEditText.getText().toString().trim())) {
            showToast(R.string.txt_user_pass_cannot_be_empty);
            return false;
        }
        Pattern p = Pattern.compile("^[0-9]{11}$");
        Matcher m = p.matcher(mUserNameEditText.getText().toString().trim());
        if (!m.matches()) {
            showToast(R.string.txt_user_name_format_error);
            return false;
        }
        Pattern p1 = Pattern.compile("^.{6,16}$");
        Matcher m1 = p1.matcher(mUserPassEditText.getText().toString().trim());
        if (!m1.matches()) {
            showToast(R.string.txt_user_pass_length_error);
            return false;
        }
        return true;
    }
}